package jobtc;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Arrays;

public class Words extends JFrame {

	String[] word =new String[100];//금지어 저장 배열
	int count = 0 ; // 저장된 금지어의 갯수
	
	private JPanel contentPane;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JTextField danger;
	private JTextField findStr;
	private JButton btnNewButton;
	private JButton btnNewButton_1;
	private JScrollPane scrollPane;
	private JTextArea result;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					Words frame = new Words();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Words() {
		setTitle("금지어 추가 및 검색");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 563, 552);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLblNewLabel());
		contentPane.add(getLblNewLabel_1());
		contentPane.add(getDanger());
		contentPane.add(getFindStr());
		contentPane.add(getBtnNewButton());
		contentPane.add(getBtnNewButton_1());
		contentPane.add(getScrollPane());
	}
	
	public void find(){
		boolean flag=false; //금지어가 포함되어 있지 않은 경우
		int cnt=0; // 포함된 금지어의 갯수
		String w = "";
		String str = findStr.getText();
		if(str.equals("")) return;
		for(int i=0 ; i<count ; i++){
			if(str.contains(word[i])){
				flag=true;
				cnt++;
				w += word[i] + " ";
			}
		}
		
		if(flag){//금지어가 한개 이상 포함된 경우
			result.append("금지어가 포함되어 있습니다.\n");
			result.append("포함된 금지어 수 : " + cnt);
			result.append("포함된 금지어 : " + w);
		}else{
			result.append("금지어가 포함되어 있지 않습니다.\n");
		}
		result.setCaretPosition(result.getText().length());
	}
	
	
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("금지어");
			lblNewLabel.setBounds(42, 68, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("검색어");
			lblNewLabel_1.setBounds(42, 112, 57, 15);
		}
		return lblNewLabel_1;
	}
	private JTextField getDanger() {
		if (danger == null) {
			danger = new JTextField();
			danger.setBounds(83, 65, 177, 21);
			danger.setColumns(10);
		}
		return danger;
	}
	private JTextField getFindStr() {
		if (findStr == null) {
			findStr = new JTextField();
			findStr.setBounds(83, 109, 313, 21);
			findStr.setColumns(10);
		}
		return findStr;
	}
	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("금지어 추가");
			btnNewButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					if(danger.getText().equals("")){
						return;
					}
					
					String[] temp = danger.getText().split(" ");
					for(int i=0 ; i<temp.length ; i++){
						word[count] = temp[i];
						count++;
						if(count>99){
							for(int j=0 ; j<99; j++){
								word[j] = word[j+1];
							}
							count=99;
						}
					}
					result.append("금지어가 추가되었습니다.\n");
					result.append("추가된 금지어 : " 
									+ Arrays.toString(temp) + "\n");
					result.setCaretPosition(result.getText().length());
					
				}
			});
			btnNewButton.setBounds(268, 64, 97, 23);
		}
		return btnNewButton;
	}
	private JButton getBtnNewButton_1() {
		if (btnNewButton_1 == null) {
			btnNewButton_1 = new JButton("검색");
			btnNewButton_1.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					find();
				}
			});
			btnNewButton_1.setBounds(408, 108, 97, 23);
		}
		return btnNewButton_1;
	}
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setBounds(42, 166, 462, 338);
			scrollPane.setViewportView(getResult());
		}
		return scrollPane;
	}
	private JTextArea getResult() {
		if (result == null) {
			result = new JTextArea();
		}
		return result;
	}
}
